package iamdev.me.ytds.data;

import com.alibaba.fastjson.JSON;
import iamdev.me.ytds.entity.Doc;
import iamdev.me.ytds.utils.HashUtil;
import iamdev.me.ytds.utils.StringUtils;
import iamdev.me.ytds.utils.TikaUtils;
import org.apache.commons.io.FileUtils;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * Title: DataPrepare.java
 *
 * @author zxc
 * @time 2018/6/23 下午6:01
 */
public class DataPrepare {

    public void scanDoc(String path,File saveFile) throws IOException {
        File dir =new File(path);
        File [] files = dir.listFiles(pathname -> {
            if(pathname.isFile()){
                String name = pathname.getName().toLowerCase();
                if(name.endsWith(".doc")){
                    return true;
                }
                if(name.endsWith(".pdf")){
                    return true;
                }
                if(name.endsWith(".docx")){
                    return true;
                }
                if(name.endsWith(".ppt")){
                    return true;
                }
                if(name.endsWith(".pptx")){
                    return true;
                }
                if(name.endsWith(".txt")){
                    return true;
                }
            }
            return false;
        });
        int length = 40;
        if(files.length<1024){
            length = files.length;
        }
        List<Doc> docList=new ArrayList<>();
        for(int i=0;i<length;i++){
            String content = TikaUtils.parseContent(files[i]);
            Doc doc =new Doc();
            doc.setDocContent(content);
            doc.setDocId(i);
            doc.setDocCreateDate(new Date());
            doc.setDocDelete(0);
            doc.setDocModifyDate(new Date());
            doc.setDocName(files[i].getName());
            doc.setDocOpen(0);
            doc.setDocSha256(HashUtil.sha256(files[i]));
            doc.setDocSize(files[i].length());
            doc.setDocTitle(files[i].getName());
            doc.setDocType(StringUtils.getFileSuffix(files[i].getName()));
            doc.setDocUserId(1);
            docList.add(doc);
        }
        FileUtils.writeStringToFile(saveFile, JSON.toJSONString(docList,true),"utf-8");
    }


    @Test
    public void prepare() throws IOException {
        scanDoc("/Users/zxc/文档",new File("testfile.json"));
    }
}
